package mcmt.controller;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.Node;

import mcmt.model.Item;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class FileController {

	private String homeCode = "";
	private String foreignCode;
	private String fileURL;
	private double currencyValue;
	private static final String XML_FILE_ADDRESS_PART1 = "http://themoneyconverter.com/rss-feed/";
	private static final String XML_FILE_ADDRESS_PART2 = "/rss.xml";
	private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
	private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
	private static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
	private Item item;

	public void setFileURL(String homecode, String foreignCode, double value) {

		if (!this.homeCode.equals(homecode)) {
			this.homeCode = homecode;
			this.fileURL = XML_FILE_ADDRESS_PART1 + this.homeCode
					+ XML_FILE_ADDRESS_PART2;
			// System.out.println("ZMIENIAM!");
		}
		this.foreignCode = foreignCode;
		this.currencyValue = value;
		this.item = new Item();
		// System.out.println(this.fileURL);
		// System.out.println(this.currencyValue);
		this.readXMLFile();
	}

	private void readXMLFile() {

		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
				.newInstance();
		docBuilderFactory.setNamespaceAware(true);
		docBuilderFactory.setValidating(true);
		docBuilderFactory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
		docBuilderFactory.setAttribute(JAXP_SCHEMA_SOURCE, new File(
				"schema.xsd"));

		DocumentBuilder docBuilder = null;
		Document doc = null;
		try {
			docBuilder = docBuilderFactory.newDocumentBuilder();
			doc = docBuilder.parse(this.fileURL);
			doc.getDocumentElement().normalize();
		} catch (ParserConfigurationException | SAXException | IOException e) {
			System.err.println(e.getMessage());
		}

		NodeList items = doc.getElementsByTagName("item");

		for (int i = 0; i < items.getLength(); i++) {

			if (items.item(i).getNodeType() == Node.ELEMENT_NODE) {
				if (isProperNode(items.item(i).getTextContent())) {

					NodeList properNode = items.item(i).getChildNodes();
					this.item.setTitle(properNode.item(1).getTextContent());
					this.item.setLink(properNode.item(3).getTextContent());
					this.item.setPubDate(properNode.item(5).getTextContent());
					String desc = properNode.item(7).getTextContent();
					this.item.setDescription(desc);
					this.item.setRegion(properNode.item(9).getTextContent());
					this.item.setRate(this.convertDouble(desc));
					this.item.setUnit(this.currencyValue);
					// System.out.println(this.item);
					return;
				}
			}
		}

	}

	public double calculateResult() {
		return this.item.getUnit() * this.item.getRate();
	}

	private boolean isProperNode(String str) {
		Pattern p = Pattern.compile(this.foreignCode);
		Matcher m = p.matcher(str);
		if (m.find()) {
			return true;
		}
		return false;
	}

	private double convertDouble(String str) {
		Pattern p = Pattern.compile("(\\d+(?:\\.\\d+))");
		Matcher m = p.matcher(str);
		if (m.find()) {
			return Double.parseDouble(m.group(1));
		}
		return 0.0;
	}

	public Item getItem() {
		return item;
	}
}
